package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {


    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult findPage(SensitiveDto sensitiveDto) {
        //1参数检查
        if (sensitiveDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //分页参数
        sensitiveDto.checkParam();
        //分页模糊查询
        Page page=new Page(sensitiveDto.getPage(),sensitiveDto.getSize());
        LambdaQueryWrapper<AdSensitive> lambdaQueryWrapper=new LambdaQueryWrapper();
        if (StringUtils.isNotBlank(sensitiveDto.getName())){
            lambdaQueryWrapper.like(AdSensitive::getSensitives,sensitiveDto.getName());
        }
        IPage page1 =page(page,lambdaQueryWrapper);
        //封装返回结果
        ResponseResult responseResult=new PageResponseResult(sensitiveDto.getPage(),sensitiveDto.getSize(),(int)page1.getTotal());
        responseResult.setData(page1.getRecords());
        return responseResult;
    }
    //添加
    @Override
    public ResponseResult add(AdSensitive adSensitive) {
        //检查参数
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        adSensitive.setCreatedTime(new Date());
        save(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    //修改
    @Override
    public ResponseResult edit(AdSensitive adSensitive) {
        if (adSensitive==null || adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(adSensitive);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
    //删除
    @Override
    public ResponseResult delete(Integer id) {
        //检查参数
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断当前参数是否存在
        AdSensitive adSensitive=getById(id);
        if (adSensitive==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        removeById(id);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public List<AdSensitive> findAllSensitive() {
        return adSensitiveMapper.findAllSensitive();
    }

}
